package com.zqh.myfmmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zqh.myfmmall.constants.RedisExpireTime;
import com.zqh.myfmmall.dao.ProductCommentsDao;
import com.zqh.myfmmall.entity.Product;
import com.zqh.myfmmall.entity.ProductComments;
import com.zqh.myfmmall.entity.ProductImg;
import com.zqh.myfmmall.entity.Users;
import com.zqh.myfmmall.redis.RedisUtil;
import com.zqh.myfmmall.result.Result;
import com.zqh.myfmmall.service.ProductCommentsService;
import com.zqh.myfmmall.service.UsersService;
import com.zqh.myfmmall.vo.ProductCommentsVO;
import com.zqh.myfmmall.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品评价 (ProductComments)表服务实现类
 *
 * @author KONO 曾淇杭哒！
 * @since 2022-01-20 20:53:00
 */
@Service("productCommentsService")
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsDao, ProductComments>
        implements ProductCommentsService{
    @Resource
    private UsersService usersService;
    @Autowired
    private RedisUtil redisUtil;

    //还没解决缓存击穿问题，按照写好的那些改下就好
    @Override
    public Map<String, Integer> getProductCommentsCountsByProductId(String productId) {
        int totalCount = count(new QueryWrapper<ProductComments>().eq("product_id", productId));
        int goodCount = count(new QueryWrapper<ProductComments>().eq("product_id", productId).eq("comm_type", 1));
        int middleCount = count(new QueryWrapper<ProductComments>().eq("product_id", productId).eq("comm_type", 0));
        int badCount = count(new QueryWrapper<ProductComments>().eq("product_id", productId).eq("comm_type", -1));
        int goodRate = 0;
        if (totalCount != 0) {
            goodRate = goodCount / totalCount * 100;
        }
        HashMap<String, Integer> map = new HashMap<>();
        map.put("total", totalCount);
        map.put("goodTotal", goodCount);
        map.put("midTotal", middleCount);
        map.put("badTotal", badCount);
        map.put("goodRate", goodRate);
        return map;
    }

    //因为商品的评价可能为空需要注意缓存穿透问题
    @Override
    public List<ProductCommentsVO> getByProductId(String productId) throws JsonProcessingException {
        List<ProductCommentsVO> productCommentsVos = null;
        productCommentsVos = (List<ProductCommentsVO>) redisUtil.get("productCommentsVo:" + productId);
        //categories-list缓存不为空
        if (productCommentsVos != null) {
            return productCommentsVos;
        }
        //缓存为空则查询数据库并更新缓存
        else {
            List<ProductComments> comments = query().eq("product_id", productId).list();
            for (ProductComments comment : comments) {
                Users user = usersService.getById(comment.getUserId());
                ProductCommentsVO productCommentsVO = new ProductCommentsVO(comment, user);
                productCommentsVos.add(productCommentsVO);
            }
            //更新缓存
            redisUtil.set("productCommentsVo:" + productId, comments,
                    RedisExpireTime.PRODUCT.getTime(), RedisExpireTime.COMMENTS.getTimeUnit());
            return productCommentsVos;
        }
    }
}

